<?php
namespace app\common\model;

use think\Db;
use think\Model;

class NavNews extends Model
{
	protected $insert = ['create_time'];

    /**
     * 创建时间
     * @return bool|string
     */
    protected function setCreateTimeAttr()
    {
        return date('Y-m-d H:i:s');
    }

    /**
     * 获得新闻中心的导航栏id nav_id
     * @return int
     */
    public function findnewsid()
    {
    	$newsid = Db('nav')->select();
    	$list = array2level($newsid,3);
    	return $list;
    }

    /**
     * 获得新闻列表
     * @return int
     */
    public function getnewslist($keyword,$cid,$page)
    {
    	$map = [];
    	if (!empty($keyword)) {
            $map['title'] = ['like', "%{$keyword}%"];
        }
        if(!empty($cid)){
        	$map['nav_id'] = $cid;
        }

    	//最新动态id写死了,记得修改
    	$newslist = $this->where($map)->where('nav_id',3)->order(['create_time' => 'DESC'])->paginate(10, false, ['page' => $page]);
    	$i = 0;
    	$arr = [];
    	for(;$i<count($newslist);$i++){
    		$nav_name = Db('nav')->where('id',$newslist[$i]['nav_id'])->column('name'); 
            $newslist[$i]['content'] = self::getContent($newslist[$i]['content']);
    		$newslist[$i]['nav_id']  = $nav_name[0];
    	}
    	return $newslist;
    }

    /**
     * 获得导航栏的名称
     * @return string
     */
    public function getNavName()
    {
    	return $this->belongsto('nav','nav_id','id')->field('name');
    }


    /**
     * 删除新闻的thumb和图片
     * @return 1
     */
    public function deletephoto($id)
    {
    	$fileImg = $this->where('id',$id)->value('thumb');
    	//删除缩略图
    	if($fileImg){
            if(file_exists('./'.$fileImg)){
                unlink('./'.$fileImg);
            }
        }
        //删除图片集合
        $photo = $this->where('id',$id)->value('photo');

        if($photo){
        	$arr = getphoto($photo);
        	//循环删除图集图片
        	for($i = 0;$i<count($arr);$i++){
        		if(file_exists('./'.$arr[$i])){
                	unlink('./'.$arr[$i]);
            	}
        	}           
        }
        return 1;
    }

    /**
     * 获得关于我们的导航栏id nav_id
     * @return int
     */
    public function findaboutid()
    {
        $newsid = Db('nav')->select();
        $list = array2level($newsid,12);
        return $list;
    }

    /**
     * 获得关于我们的新闻列表
     * @return int
     */
    public function getaboutlist($keyword,$cid,$page)
    {
        $map = [];
        if (!empty($keyword)) {
            $map['title'] = ['like', "%{$keyword}%"];
        }
        if(!empty($cid)){
            $map['nav_id'] = $cid;
        }

        $arr = $this->getNavlist(12);
        if($arr){
            $map['nav_id'] = ['in',$arr];
        }
        // $newslist = self::with('getNavName')->where($map)->select();

        $newslist = $this->where($map)->order(['create_time' => 'DESC'])->paginate(10, false, ['page' => $page]);
        $i = 0;
        $arr = [];
        for(;$i<count($newslist);$i++){
            $nav_name = Db('nav')->where('id',$newslist[$i]['nav_id'])->column('name'); 
            $newslist[$i]['nav_id'] = $nav_name[0];
        }
        return $newslist;
    }

    /**
     * 获得产品与服务的导航栏id nav_id
     * @return int
     */
    public function findproductid()
    {
        $newsid = Db('nav')->select();
        $list = array2level($newsid,4);
        return $list;
    }

    /**
     * 获得产品与服务的的新闻列表
     * @return int
     */
    public function getproductlist($keyword,$cid,$page)
    {
        $map = [];
        if (!empty($keyword)) {
            $map['title'] = ['like', "%{$keyword}%"];
        }
        if(!empty($cid)){
            $map['nav_id'] = $cid;
        }

        $arr = $this->getNavlist(4);
        if($arr){
            $map['nav_id'] = ['in',$arr];
        }
        // $newslist = self::with('getNavName')->where($map)->select();

        $newslist = $this->where($map)->order(['create_time' => 'DESC'])->paginate(10, false, ['page' => $page]);
        $i = 0;
        $arr = [];
        for(;$i<count($newslist);$i++){
            $nav_name = Db('nav')->where('id',$newslist[$i]['nav_id'])->column('name'); 
            $newslist[$i]['nav_id'] = $nav_name[0];
        }
        return $newslist;
    }



    /**
     * 获得新闻列表
     * @return arr
     */
    public function getNavlist($id)
    {
        $newsid = Db('nav')->select();
        $arr = array2array($newsid,$id);
        return $arr;
    }


    /**
     * 获得新闻列表
     * @return str
     */
    static function getContent($str,$length=30)
    {
        $tmpstr = strip_tags($str);
        $tmpstr = htmlentities($tmpstr);
        $strlen = mb_strlen($tmpstr);
        $tmpstr = mb_substr($tmpstr,0,$length,'utf-8');
        return $tmpstr.($strlen > $length ? '...' : '');
    }


}